package com.lebang.controller.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.PkgBlackLog;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.PkgBlackLogExample;
import com.lebang.model.MemberSdkErrorModel;
import com.lebang.model.enums.buss.UserStatus;
import com.lebang.service.buss.MemberService;
import com.lebang.service.sys.PkgBlackLogService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


/**
 * pkg_black_log
 *
 * @author hwq
 * @date 2020-07-21
 */
@Controller
@RequestMapping("/sys/pkgblacklog")
public class PkgBlackLogController {
    @Autowired
    private PkgBlackLogService baseService;
    @Autowired
    private MemberService memberService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/sys/pkgblacklog/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        PkgBlackLog vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new PkgBlackLog();
        }
        model.addAttribute("item", vo);
        return "/sys/pkgblacklog/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(PkgBlackLog vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,
                           String phone) {
        PkgBlackLogExample example = new PkgBlackLogExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        PkgBlackLogExample.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(phone)){
            LbbMember member=memberService.selectByPhone(phone);
            if(member!=null){
                criteria.andMemberIdEqualTo(member.getMemberId());
            }
            example.or(example.createCriteria().andImeiEqualTo(phone));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }







}
